package com.wangzhe.builder;

import com.sun.org.apache.bcel.internal.classfile.Code;
import com.wangzhe.bean.Constants;
import com.wangzhe.bean.FieldInfo;
import com.wangzhe.bean.TableInfo;
import com.wangzhe.utils.DateTimeUtil;
import com.wangzhe.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @author 王哲
 * @ClassName BuilderService
 * @create 2023--六月--下午2:53
 * @Description 构造Service类
 * @Version V1.0
 */
public class BuilderService {
    private static Logger logger = LoggerFactory.getLogger(BuilderService.class);

    public static void builderService(TableInfo tableInfo) {
        File folder = new File(Constants.PACKAGE_SERVICE_PATH);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        File file = new File(folder, tableInfo.getBeanName() + "Service.java");

        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        try {
            os = new FileOutputStream(file);
            osw = new OutputStreamWriter(os, "utf-8");
            bw = new BufferedWriter(osw);

            bw.write("package " + Constants.PACKAGE_SERVICE + "; ");
            bw.newLine();
            bw.newLine();
            bw.write("import java.util.List;");
            bw.newLine();
            bw.write("import " + Constants.POJO_BASE + "." + tableInfo.getBeanName() + ";");
            bw.newLine();
            bw.write("import " + Constants.EXAMPLE_PATH + "." + tableInfo.getBeanParamName() + ";");
            bw.newLine();
            bw.write("import " + Constants.VO_PATH + ".PaginationResultVO;");
            bw.newLine();
            bw.newLine();

            BuilderComment.createClassComment(bw, tableInfo.getTableName() + "ServiceImpl");

            bw.write("public interface " + tableInfo.getBeanName() + "Service {");
            bw.newLine();
            bw.newLine();
            BuilderComment.createMethodComment(bw, "根据条件查询列表");
            bw.write("\tList<" + tableInfo.getBeanName() + "> findListByParam(" + tableInfo.getBeanParamName() + " query);");
            bw.newLine();
            bw.newLine();
            BuilderComment.createMethodComment(bw, "根据条件查询数量");
            bw.write("\tInteger findCountByParam(" + tableInfo.getBeanParamName() + " query);");
            bw.newLine();
            bw.newLine();
            BuilderComment.createMethodComment(bw, "分页查询");
            bw.write("\tPaginationResultVO<" + tableInfo.getBeanName() + "> findListByPage(" + tableInfo.getBeanParamName() + " query);");
            bw.newLine();
            bw.newLine();
            BuilderComment.createMethodComment(bw, "新增");
            bw.write("\tInteger add(" + tableInfo.getBeanName() + " bean );");
            bw.newLine();
            bw.newLine();
            BuilderComment.createMethodComment(bw, "批量新增");
            bw.write("\tInteger addBatch( List<" + tableInfo.getBeanName() + "> listBean );");
            bw.newLine();
            bw.newLine();
            BuilderComment.createMethodComment(bw, "批量新增/修改");
            bw.write("\tInteger addOrUpdateBatch( List<" + tableInfo.getBeanName() + "> listBean );");
            bw.newLine();
            bw.newLine();

            //拿到主键
            Map<String, List<FieldInfo>> keyIndexMap = tableInfo.getKeyIndexMap();
            for (Map.Entry<String, List<FieldInfo>> stringListEntry : keyIndexMap.entrySet()) {
                List<FieldInfo> value = stringListEntry.getValue();

                Integer index = 0;
                StringBuilder sb = new StringBuilder();
                StringBuilder methodParams = new StringBuilder();

                for (FieldInfo fieldInfo : value) {
                    index++;
                    sb.append(StringUtils.upperCaseFirst(fieldInfo.getPropertyName()));
                    if (index < value.size()) {
                        sb.append("And");
                    }
                    methodParams.append(fieldInfo.getFieldJavaType() + " " + fieldInfo.getPropertyName());
                    if (index < value.size()) {
                        methodParams.append(",");
                    }
                }
                //查询语句生成
                BuilderComment.createMethodComment(bw, "根据" + sb.toString() + "查询");
                bw.write("\t "+tableInfo.getBeanName()+" get"+tableInfo.getBeanName()+"By" + sb.toString() + "(" + methodParams + ");");
                bw.newLine();

                //删除语句生成
                BuilderComment.createMethodComment(bw, "根据" + sb.toString() + "删除");
                bw.write("\t Integer delete"+tableInfo.getBeanName()+"By" + sb.toString() + "(" + methodParams + ");");
                bw.newLine();

                //更新语句生成
                BuilderComment.createMethodComment(bw, "根据" + sb.toString() + "更新");
                bw.write("\t Integer update"+tableInfo.getBeanName()+"By" + sb.toString() + "( "+tableInfo.getBeanName()+" bean, " + methodParams + ");");
                bw.newLine();
            }

            bw.write("}");
            bw.newLine();
            bw.newLine();

        } catch (Exception e) {
            logger.info("创建service失败:{}", e.getMessage());
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
                if (osw != null) {
                    osw.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
